/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.hazard.enterprises.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.hazard.enterprises.dto.HazEnterprisesAlarmConfigExcelDTO;
import com.hopes.bizz.hazard.enterprises.entity.HazEnterprisesAlarmConfigEntity;
import com.hopes.bizz.hazard.enterprises.service.HazEnterprisesAlarmConfigService;
import com.hopes.bizz.hazard.enterprises.vo.HazEnterprisesAlarmConfigExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 企业风险预警设置
 *
 * @author daijie
 * @date 2024-02-01 14:47:55
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/enterprisesAlarmConfig")
@Tag(description = "enterprisesAlarmConfig", name = "企业风险预警设置管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HazEnterprisesAlarmConfigController {

    private final HazEnterprisesAlarmConfigService hazEnterprisesAlarmConfigService;

    /**
     * 分页查询
     *
     * @param page                      分页对象
     * @param hazEnterprisesAlarmConfig 企业风险预警设置
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('haz_enterprisesAlarmConfig_view')")
    public R gethazEnterprisesAlarmConfigPage(Page page, HazEnterprisesAlarmConfigEntity hazEnterprisesAlarmConfig) {
        LambdaQueryWrapper<HazEnterprisesAlarmConfigEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(hazEnterprisesAlarmConfig.getOrgId()), HazEnterprisesAlarmConfigEntity::getOrgId, hazEnterprisesAlarmConfig.getOrgId());
        wrapper.eq(HazEnterprisesAlarmConfigEntity::getDefaultFlag, false);

        return R.ok(hazEnterprisesAlarmConfigService.page(page, wrapper));
    }

    @Operation(summary = "查询全局配置" , description = "查询全局配置" )
    @GetMapping("/global" )
    @PreAuthorize("@pms.hasPermission('haz_enterprisesAlarmConfig_view')" )
    public R getPollAlarmConfigGlobal() {
        LambdaQueryWrapper<HazEnterprisesAlarmConfigEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(HazEnterprisesAlarmConfigEntity::getDefaultFlag, true);

        return R.ok(hazEnterprisesAlarmConfigService.getOne(wrapper));
    }


    /**
     * 通过id查询企业风险预警设置
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('haz_enterprisesAlarmConfig_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(hazEnterprisesAlarmConfigService.getById(id));
    }

    /**
     * 新增企业风险预警设置
     *
     * @param hazEnterprisesAlarmConfig 企业风险预警设置
     * @return R
     */
    @Operation(summary = "新增企业风险预警设置", description = "新增企业风险预警设置")
    @SysLog("新增企业风险预警设置")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('haz_enterprisesAlarmConfig_add')")
    public R save(@RequestBody HazEnterprisesAlarmConfigEntity hazEnterprisesAlarmConfig) {
        hazEnterprisesAlarmConfig.setDefaultFlag(false);
        return R.ok(hazEnterprisesAlarmConfigService.save(hazEnterprisesAlarmConfig));
    }

    /**
     * 修改企业风险预警设置
     *
     * @param hazEnterprisesAlarmConfig 企业风险预警设置
     * @return R
     */
    @Operation(summary = "修改企业风险预警设置", description = "修改企业风险预警设置")
    @SysLog("修改企业风险预警设置")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('haz_enterprisesAlarmConfig_edit')")
    public R updateById(@RequestBody HazEnterprisesAlarmConfigEntity hazEnterprisesAlarmConfig) {
        return R.ok(hazEnterprisesAlarmConfigService.updateById(hazEnterprisesAlarmConfig));
    }

    /**
     * 通过id删除企业风险预警设置
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除企业风险预警设置", description = "通过id删除企业风险预警设置")
    @SysLog("通过id删除企业风险预警设置")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('haz_enterprisesAlarmConfig_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(hazEnterprisesAlarmConfigService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('haz_enterprisesAlarmConfig_export')")
    public List<HazEnterprisesAlarmConfigExcelVO> template() {
        return hazEnterprisesAlarmConfigService.exportExcel(Collections.singletonList(new HazEnterprisesAlarmConfigEntity()));
    }

    /**
     * 导出excel 表格
     *
     * @param hazEnterprisesAlarmConfigExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('haz_enterprisesAlarmConfig_export')")
    public List<HazEnterprisesAlarmConfigExcelVO> export(HazEnterprisesAlarmConfigExcelDTO hazEnterprisesAlarmConfigExcelDto) {
        return hazEnterprisesAlarmConfigService.exportExcel(hazEnterprisesAlarmConfigService.list());
    }

    /**
     * 导入excel 表格
     *
     * @param hazEnterprisesAlarmConfigExcelDtoList 查询条件
     * @param bindingResult                         错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('haz_enterprisesAlarmConfig_export')")
    public R importExcel(@RequestExcel List<HazEnterprisesAlarmConfigExcelDTO> hazEnterprisesAlarmConfigExcelDtoList, BindingResult bindingResult) {
        return hazEnterprisesAlarmConfigService.importExcel(hazEnterprisesAlarmConfigExcelDtoList, bindingResult);
    }
}